<?php
namespace app\index\model;
use think\Model;

class Issue extends Model
{
    protected $resultSetType = 'collection';
    /**
     * [查询商品并计算销量]商品信息
     */
    public function commodity()
    {
        $re=model('Issue')
            ->alias('a')
            ->join('sp_indent b','a.id=b.issueid','left')
            ->join('sp_brand c','a.brandid=c.id','left')
            ->join('sp_classify d','a.classifyid=d.id','left')
            ->field('a.*,sum(b.number) as xiaoliang,c.name as brand,d.name as classify')
            ->whereor(['stoppre'=>['<',date('Y-m-d H:i:s',time())],'ispre'=>['=',0]])
            ->group('a.id')
            ->select();
        foreach($re as $k)
        {
            $arr[]=$k->toArray();
        }
        return $arr;
    }
    /**
     * [查询商品并计算销量]预售商品信息
     */
    public function PreData()
    {
        $re=model('Issue')
            ->alias('a')
            ->join('sp_indent b','a.id=b.issueid','left')
            ->join('sp_brand c','a.brandid=c.id','left')
            ->join('sp_classify d','a.classifyid=d.id','left')
            ->field('a.*,sum(b.number) as xiaoliang,c.name as brand,d.name as classify')
            ->where(['stoppre'=>['>',date('Y-m-d H:i:s',time())],'ispre'=>['=',1]])
            ->group('a.id')
            ->select();
        foreach($re as $k)
        {
            $arr[]=$k->toArray();
        }
        return $arr;
    }
    /**
     * 执行添加商品信息
     * @param $data
     * @param $image
     * @return false|int
     */
    public function adddo($data,$image)
    {
        if(!empty($image)){
            //添加图片
            $time=date('Ymd',time());
            $filename=time().'image';
            if(!file_exists(ROOT_PATH . 'public' . DS .'static'. DS .'img_issue'))
            {
                //创建文件
                mkdir(ROOT_PATH . 'public' . DS .'static'. DS .'img_issue');
            }
            $info=$image->move(ROOT_PATH . 'public' . DS .'static'. DS .'img_issue'.DS.$time,$filename);
            $data['photo'] = $time.DS.$info->getFileName();
            $data = new Issue($data);
            return $data->allowField(true)->save();
        }else
        {
            return 0;
        }
    }

    /**
     * 查看修改商品的信息
     * @param $id
     * @return array
     */
    public function mod($id)
    {
        $data =Issue::where('a.id',$id)
            ->alias('a')
            ->join('brand b','a.brandid = b.id','left')
            ->join('classify c','a.classifyid = c.id','left')
            ->column('*,a.name iname,a.id aid');
        foreach($data as $key)
        {
            $data = $key;
        }
        if($data['ispre']==1){
            $data['stoppre']=TimeAudit($data['stoppre']);
            $data['startpre']=TimeAudit($data['startpre']);
        }
        return $data;
    }

    /**
     * 执行修改商品信息的操作
     * @param $data
     * @param $image
     * @param $id
     * @return Issue
     * @throws \think\exception\DbException
     */
    public function moddo($data,$image,$id)
    {
        if ($image)
        {
            //删除原图片
            $data = Issue::get($id)->getdata();
            $path=ROOT_PATH . 'public' . DS . 'static' . DS . 'img_issue'. DS .$data['photo'];
            if(file_exists($path)){
                @unlink($path);
                clearstatcache();
            }
            //上传新图片
            $time=date('Ymd',time());
            $filename=time().'image';
            if(!file_exists(ROOT_PATH . 'public' . DS .'static'. DS .'img_issue'))
            {
                //创建文件
                mkdir(ROOT_PATH . 'public' . DS .'static'. DS .'img_issue');
            }
            $info=$image->move(ROOT_PATH . 'public' . DS .'static'. DS .'img_issue'.DS.$time,$filename);
            $data['photo']=$time.DS.$info->getFileName();
            $issue = new Issue();

            $brandid=$data['brandid'];
            $classifyid=$data['classifyid'];
            $name=$data['name'];
            $attr=$data['attr'];
            $inventory= $data['inventory'];
            $putaway=$data['putaway'];
            $bazaar=$data['bazaar'];
            $stand=$data['stand'];
            $active=$data['active'];
            $cost=$data['cost'];
            $audit=  $data['audit'];

            return $issue->where('id', $id)->update([
                'brandid'     => $brandid  ,
                'classifyid'    =>   $classifyid,
                'name'            => $name  ,
                'photo'          =>    $time.DS.$info->getFileName(),
                'attr'               =>  $attr ,
                'inventory'   =>  $inventory,
                'putaway'     =>   $putaway,
                'bazaar'         =>  $bazaar ,
                'stand'           =>  $stand ,
                'active'          =>   $active,
                'cost'             =>   $cost ,
                'audit'           => $audit,
            ]);
        }else
        {
            $issue = new Issue();
            $brandid=$data['brandid'];
            $classifyid=$data['classifyid'];
            $name=$data['name'];
            $attr=$data['attr'];
            $inventory= $data['inventory'];
            $putaway=$data['putaway'];
            $bazaar=$data['bazaar'];
            $stand=$data['stand'];
            $active=$data['active'];
            $cost=$data['cost'];
            $audit=  $data['audit'];

            return $issue->where('id', $id)->update([
                'brandid'     => $brandid  ,
                'classifyid'    =>   $classifyid,
                'name'            => $name  ,
                'attr'               =>  $attr ,
                'inventory'   =>  $inventory,
                'putaway'     =>   $putaway,
                'bazaar'         =>  $bazaar ,
                'stand'           =>  $stand ,
                'active'          =>   $active,
                'cost'             =>   $cost ,
                'audit'           => $audit,
            ]);
        }
    }
    public function addcommodity($data)
    {
        $re=new Issue($data);
        return $res=$re->allowField(true)->save();
    }
    public function getAttrAttr($value)
    {
        $attr=[1=>'推荐',2=>'精品',3=>'热销',4=>'新品'];
        return $attr[$value];
    }
    public function getAuditAttr($value)
    {
        $audit=[0=>'未通过',1=>'通过'];
        return $audit[$value];
    }
    public function getPutawayAttr($value)
    {
        $putaway=[0=>'下架',1=>'上架'];
        return $putaway[$value];
    }
    //计算商品总数
    public function zong(){
        $res=$this->field('count(id) as count')->find();
        return $res['count'];
    }
}
?>
